<template>
  <section v-loading="loading" class="cms-body flex">
    <!-- 树组件 -->
    <cms-tree @change="getChannelId" :copy="false" :hasContent='false'></cms-tree>
    <div class="cms-content-right">
      <!-- 频道 -->
      <div class="cms-list-header clearfix flex" v-show="breadState">
        <div class="bread-box cms-left">
          <label class="">当前频道:</label>
          <ul class="type-bread">
            <li class="bread-items" v-for="(item,index) in breadItems" :key="index">
              <a href="javascript:void(0)" @click="clickBread(item.id,index)">{{item.name}}</a>
            </li>
          </ul>
        </div>
      </div>
      <div class="cms-list-header clearfix flex" style="padding:0 24px 16px 24px;flex-wrap:wrap">
        <div style="margin-top:16px;flex:auto" class="content-flex-right">
          <el-tag @click="setTime('today')" :effect="choose==1?'dark':'light'" style="cursor: pointer;">今天</el-tag>
          <el-tag @click="setTime('sevenday')" :effect="choose==2?'dark':'light'" style="cursor: pointer;">近7天</el-tag>
          <el-tag @click="setTime('onemonth')" :effect="choose==3?'dark':'light'" style="cursor: pointer;">近一月</el-tag>
          <el-tag @click="setTime('threemonth')" :effect="choose==4?'dark':'light'" style="cursor: pointer;">近三月
          </el-tag>
          <el-date-picker v-model="params.startTime" type="date" placeholder="开始时间" class="cms-width"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
          <el-date-picker v-model="params.endTime" type="date" placeholder="结束时间" class="cms-width"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
          <el-button @click="goQuery()">查询</el-button>
          <el-button @click="exportExcel2()">导出</el-button>
        </div>
      </div>
      <el-table :data="tableData" stripe @selection-change="checkIds">
        <el-table-column prop="id" label="id" width="60" align="center"></el-table-column>
        <el-table-column label="频道名称" prop="channelName" align="center"></el-table-column>
        <el-table-column label="总访问量（PV）" prop="browseNum" align="center" width="200"></el-table-column>
        <el-table-column label="IP数量" prop="ipNum" align="center" width="200"></el-table-column>
        <el-table-column label="图表" align="center" width="100">
          <div slot-scope="scope">
            <a href="javascript:void(0)" title="点击查看" class="cms-table-button el-icon-tubiao"
               @click="lookChart(scope.row)"
               v-perms="'/channel/chart'">
            </a>
          </div>
        </el-table-column>
      </el-table>
      <!-- 表格底部 -->
      <div class="cms-list-footer" style="padding-bottom:30px;"></div>
    </div>
  </section>
</template>

<script>
  import listMixins from '@/mixins/list'
  import axios from "axios/index";

  export default {
    mixins: [listMixins],
    data() {
      return {
        params: {
          //请求参数
          parentName: '',
          parentId: '',
          channelLevel: 0,
          modelId: '',
          https: '',
          hasCollect: '',
          startTime: '',
          endTime: ''
        },
        isShow: true,
        breadState: false,
        choose: 0,
        allTotal: 0, //分页总数
        breadItems: [
          {
            name: '根目录',
            id: ''
          }
        ], //面包屑
        channelInfo: {} //当前频道信息
      }
    },
    methods: {
      getChannelId(data, node) {
        console.log(data)
        if (data.channelLevel != 2) {
          //获取频道树点击
          this.breadState = true
          this.creatBread(node, [])
          this.params.parentId = data.id //传递频道id
          this.params.channelLevel = data.channelLevel;
          this.params.modelId = data.modelId //传递频道id
          this.params.parentName = data.name
          this.getTableData(this.params) //获取内容信息
        }
      },
      creatBread(node, arr) {
        if (node.parent != null) {
          let params = {
            name: node.data.name,
            id: node.data.id
          }
          arr.push(params)
          this.creatBread(node.parent, arr)
        } else {
          this.breadItems = arr.reverse()
          return false
        }
      },
      setTime(tag) {
        const end = new Date();
        const start = new Date();
        if (tag == "sevenday") {
          // 近7天
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          this.choose = 2;
        } else if (tag == "onemonth") {
          // 近一个月
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          this.choose = 3;
        } else if (tag == "threemonth") {
          // 近三个月
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          this.choose = 4;
        } else {
          this.choose = 1;
        }
        this.params.startTime = start.format("yyyy-MM-dd");
        this.params.endTime = end.format("yyyy-MM-dd");
        this.goQuery();
      },
      lookChart(channel) {
        this.params.channelName = channel.channelName;
        console.log(this.params)
        this.routerLink('/channelTotal/chart', 'look', channel.id, this.params)
      },
      getExcel: function (res) {
        require.ensure([], () => {
          const {export_json_to_excel} = require('../../../vendor/Export2Excel.js')
          const tHeader = ['频道Id', '频道名称', '访问总量（PV）','IP数量']
          const filterVal = ['id', 'channelName', 'browseNum','ipNum']
          const list = res
          const data = this.formatJson(filterVal, list)
          console.log(this.params)
          let channelName = this.params.parentName == "" ? "全部" : this.params.parentName;
          export_json_to_excel(tHeader, data, channelName + '频道访问统计表')
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },
      exportExcel2() {
        this.loading = true;
        this.warningMessage('数据正在处理中');
        axios.post(this.$api.channelPageList, this.params).then(res => {
          if (res.code == "200") {
            this.loading = false;
            this.getExcel(res.body)
          }
        }).catch(error => {
          this.loading = false;
        })
      },
    },
    created() {
      this.$store.dispatch('setCollapse', false) //收缩状态栏
      this.loading = false
      this.breadState = false
      if (this.$route.query.type == 'chart.vue') {
        if (this.$route.query.id == 0) {
          this.params.parentId = ''
        } else {
          this.params.parentId = this.$route.query.id
        }
      }
      this.initTableData(this.$api.channelPageList, this.params) //请求列表
    },
    mounted() {
      let clientHeight = document.body.clientHeight
      $('.cms-body').css('minHeight', clientHeight - 110 + 'px')
    }
  }
</script>

<style scoped>
  .bread-box {
    display: flex;
    display: -ms-flexbox;
    align-items: center;
  }

  .bread-box label {
    color: #5a5e66;
    font-size: 14px;
  }

  .type-bread {
    margin-right: 12px;
  }

  .bread-items {
    display: inline-block;
  }

  .bread-items a {
    color: #1276c3;
    font-size: 14px;
  }

  .bread-items a::after {
    content: '/';
    padding: 0 5px;
    color: #1276c3;
  }

  .bread-items:last-child a::after {
    content: '';
    padding: 0 5px;
    color: #1276c3;
  }

  .dialog .tree-layout {
    min-height: 400px;
    width: 90%;
  }

  .dialog .tree-layout .tree {
    width: 100%;
    height: 100%;
    overflow: auto;
    position: absolute;
    border-right: 0px solid #d4dde2;
  }

  .el-icon-tubiao {
    display: block;
    margin: 0 auto;
    width: 24px;
    height: 24px;
    border-radius: 4px;
    -webkit-transition: 0.2s;
    transition: 0.2s;
    background: url(../../../../static/images/tubiao.png) center center #ff8b53 no-repeat;
    background-size: 14px;
  }
</style>
